/*
 * Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Eclipse Public License
 * Version 1.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://opensource.org/licenses/EPL-1.0 and read it before using this file.
 */

#include "scripting.h"
#include "plugin_model.h"
#include "scripting_utils.h"


ScriptableDevice::ScriptableDevice( NPP npp )
    : NPObjectBase( npp ),
    m_item( NULL ),
    m_id( NULL ),
    m_uid( NULL ),
    m_manufacturer( NULL ),
    m_productFamily( NULL ),
    m_productName( NULL )
{
}

ScriptableDevice::~ScriptableDevice()
{
    //if ( m_id ) ::NPN_MemFree( m_id );
    //if ( m_uid ) ::NPN_MemFree( m_uid );
    //if ( m_manufacturer ) ::NPN_MemFree( m_manufacturer );
    //if ( m_productFamily ) ::NPN_MemFree( m_productFamily );
    //if ( m_productName ) ::NPN_MemFree( m_productName );
}

bool ScriptableDevice::construct( const NPVariant *args, uint32_t argCount, NPVariant *result )
{
    NPObject *obj = ::NPN_CreateObject( m_npp, GET_NPOBJECT_CLASS( ScriptableDevice ) );
    if ( !obj )
        return false;

    OBJECT_TO_NPVARIANT( obj, *result );

    return true;
}

bool ScriptableDevice::hasProperty( NPIdentifier name )
{
    return name == npid.index || name == npid.id || name == npid.uid ||name == npid.manufacturer || name == npid.productFamily || name == npid.productName;
}

bool ScriptableDevice::getProperty( NPIdentifier name, NPVariant *result )
{
    if ( !m_item ) return false;

    NPVariant& resultValue = *result;
    VOID_TO_NPVARIANT( resultValue );

    try
    {
        if ( name == npid.index )
        {
            INT32_TO_NPVARIANT( m_item->getDeviceInfo().index, resultValue );
        }
        else if ( name == npid.id )
        {
            //if ( m_id == NULL )
            m_id = ScriptingUtils::npstring( m_item->getDeviceInfo().id );

            STRINGZ_TO_NPVARIANT( m_id, resultValue );
        }
        else if ( name == npid.uid )
        {
            //if ( m_uid == NULL )
            m_uid = ScriptingUtils::npstring( m_item->getDeviceInfo().uid );

            STRINGZ_TO_NPVARIANT( m_uid, resultValue );
        }
        else if ( name == npid.manufacturer )
        {
            //if ( m_manufacturer == NULL )
            m_manufacturer = ScriptingUtils::npstring( m_item->getDeviceInfo().manufacturer );

            STRINGZ_TO_NPVARIANT( m_manufacturer, resultValue );
        }
        else if ( name == npid.productFamily )
        {
            //if ( m_productFamily == NULL )
            m_productFamily = ScriptingUtils::npstring( m_item->getDeviceInfo().productFamily );

            STRINGZ_TO_NPVARIANT( m_productFamily, resultValue );
        }
        else if ( name == npid.productName )
        {
            //if ( m_productName == NULL )
            m_productName = ScriptingUtils::npstring( m_item->getDeviceInfo().productName );

            STRINGZ_TO_NPVARIANT( m_productName, resultValue );
        }
    }
    catch (...)
    {
        return false;
    }

    return true;
}

void ScriptableDevice::disposed()
{
    m_item = NULL;
}
